* Dickstein, Ho, and Mark (2023)
* Estimating Small Group Surplus

* Preliminaries
clear
cd ./data
local acg_HL_cutoff = 1.76

* Loading and clean data

** Load data
import delimited SGSurplusEstimationFile

** Keeping subset and dropping outside options
drop if x_av == 0
keep if year == 2016
drop if fixedeffect_beta0 == 0 & x_av != 0

* Creating interations, adjusted for price sensitivity

** x_av
generate x_av_adjusted = - x_av * (alpha - psi)

** adjusted metal tiers
generate bronze_adjusted = - bronze * (alpha - psi)
generate silver_adjusted = - silver * (alpha - psi)
generate gold_adjusted = - gold * (alpha - psi)
generate platinum_adjusted = - platinum * (alpha - psi)

** age
generate ageb = - (age * bronze) * (alpha - psi)
generate ages = - (age * silver) * (alpha - psi)
generate ageg = - (age * gold) * (alpha - psi)
generate agep = - (age * platinum) * (alpha - psi)

** acg
generate acgb = - (sum_concurrent_risk * bronze) * (alpha - psi) 
generate acgs = - (sum_concurrent_risk * silver) * (alpha - psi) 
generate acgg = - (sum_concurrent_risk * gold) * (alpha - psi) 
generate acgp = - (sum_concurrent_risk * platinum) * (alpha - psi) 

** family size
generate familysizeb = - (family_size * bronze) * (alpha - psi) 
generate familysizes = - (family_size * silver) * (alpha - psi) 
generate familysizeg = - (family_size * gold) * (alpha - psi) 
generate familysizep = - (family_size * platinum) * (alpha - psi) 

** group size
generate groupsizeb = - (group_size * bronze) * (alpha - psi) 
generate groupsizes = - (group_size * silver) * (alpha - psi) 
generate groupsizeg = - (group_size * gold) * (alpha - psi) 
generate groupsizep = - (group_size * platinum) * (alpha - psi) 

** create and save a group by group file 
preserve 
keep if choice == 1
ds, has(type string)
drop `r(varlist)'
foreach var of varlist bronze_adjusted-groupsizep{
  replace `var' = `var' / (alpha - psi) 
}
collapse _all
save sumstat_means, replace
restore 
preserve 
keep if choice == 1
ds, has(type string)
drop `r(varlist)'
gen family_type = "nokids_under50"
replace family_type = "kids_under50" if ndeps > 0 & age <= 50
replace family_type = "kids_over50" if ndeps > 0 & age > 50
replace family_type = "nokids_over50" if ndeps == 0 & age > 50
ds family_type, not
foreach var of varlist bronze_adjusted-groupsizep{
  replace `var' = `var' / (alpha - psi) 
}
collapse `r(varlist)', by(family_type)
append using sumstat_means
save sumstat_means, replace
restore 
preserve 
keep if choice == 1
ds, has(type string)
drop `r(varlist)'
gen metal_tier = "bronze"
replace metal_tier = "silver" if silver == 1
replace metal_tier = "goldplat" if gold == 1 | platinum == 1
ds metal_tier, not
foreach var of varlist bronze_adjusted-groupsizep{
  replace `var' = `var' / (alpha - psi) 
}
collapse `r(varlist)', by(metal_tier)
append using sumstat_means
gen type = "all"
replace type = family_type if !missing(family_type)
replace type = metal_tier if !missing(metal_tier)
save sumstat_means, replace
restore 

* Running Multinomial Logit

** Spec 1
asclogit choice bronze_adjusted gold_adjusted platinum_adjusted, case(hhid) alternatives(constructed_plan_year) noconstant offset(v)
estimates store model1
predict fee1, xb

*** calculate sumstats
preserve 
clear
use sumstat_means
gen constructed_plan_year = "SmOf1193MNC_2016"
predict fee1, xb nooffset
predict fee1se, stdp nooffset
save sumstat_means, replace
restore

** Spec 2
asclogit choice bronze_adjusted gold_adjusted platinum_adjusted ageb ageg agep, case(hhid) alternatives(constructed_plan_year) noconstant offset(v)
estimates store model2
predict fee2, xb

*** calculate sumstats
preserve 
clear
use sumstat_means
predict fee2, xb nooffset
predict fee2se, stdp nooffset
save sumstat_means, replace
restore

** Spec 3
asclogit choice bronze_adjusted gold_adjusted platinum_adjusted ageb ageg agep acgb acgg acgp, case(hhid) alternatives(constructed_plan_year) noconstant offset(v)
estimates store model3
predict fee3, xb

*** calculate sumstats
preserve 
clear
use sumstat_means
predict fee3, xb nooffset
predict fee3se, stdp nooffset
save sumstat_means, replace
restore

** Spec 4
asclogit choice bronze_adjusted gold_adjusted platinum_adjusted ageb ageg agep acgb acgg acgp groupsizeb groupsizeg groupsizep, case(hhid) alternatives(constructed_plan_year) noconstant offset(v)
estimates store model4
predict fee4, xb

*** calculate sumstats
preserve 
clear
use sumstat_means
predict fee4, xb nooffset
predict fee4se, stdp nooffset
save sumstat_means, replace
restore

** Spec 5
asclogit choice bronze_adjusted gold_adjusted platinum_adjusted ageb ageg agep acgb acgg acgp groupsizeb groupsizeg groupsizep familysizeb familysizeg familysizep, case(hhid) alternatives(constructed_plan_year) noconstant offset(v)
estimates store model5
predict fee5, xb

*** calculate sumstats
preserve 
clear
use sumstat_means
predict fee5, xb nooffset
predict fee5se, stdp nooffset
save sumstat_means, replace
outsheet type x_av group_size age family_size sum_concurrent_risk alpha psi ndeps fee1 fee2 fee3 fee4 fee5 fee1se fee2se fee3se fee4se fee5se using sumstat_means.csv, comma replace
outsheet type x_av fee1 fee2 fee3 fee4 fee5 fee1se fee2se fee3se fee4se fee5se using "../../../../tablesandfigures/release/fee_sg_cs/fee_sumstats.csv", comma replace
restore

* Saving file with the relevant data
outsheet subscriberid year constructed_plan_year choice x_av group_size age family_size sum_concurrent_risk alpha psi ndeps v v_notiering fee1 fee2 fee3 fee4 fee5 using sg_dat_westfees.csv, comma replace

* Printing results
esttab model1 model2 model3 model4 model5, b(%10.7f) se(%10.7f) stats(N N_case) plain 
esttab model1 model2 model3 model4 model5 using "../../../../tablesandfigures/release/fee_sg_cs/model_results.csv", b(%10.7f) se(%10.7f) stats(N N_case) plain replace

* Estimating surplus 

** prelims
preserve
predict Vprime, xb
generate expV = exp(v)
generate expVprime = exp(Vprime)
generate expV_notiering = exp(v_notiering)
collapse (sum) expVprime expV expV_notiering (mean) alpha psi sum_concurrent_risk, by(subscriberid year)

** tiered prices
generate surplus_v = log(expV) / (alpha - psi)
generate surplus_vprime = log(expVprime) / (alpha - psi)
generate surplus_v_00 = log(expV + 1) / (alpha - psi)
generate surplus_vprime_00 = log(expVprime + 1) / (alpha - psi)
summarize surplus_v
local ave_surplus_v = r(mean)
summarize surplus_vprime
local ave_surplus_vprime = r(mean) 
summarize surplus_v if (alpha - psi) > .05
local ave_surplus_v_05 = r(mean) 
summarize surplus_vprime if (alpha - psi) > .05
local ave_surplus_vprime_05 = r(mean) 
summarize surplus_v_00
local ave_surplus_v_00 = r(mean) 
summarize surplus_vprime_00
local ave_surplus_vprime_00 = r(mean) 
summarize surplus_v_00 if (alpha - psi) > .05
local ave_surplus_v_00_05 = r(mean) 
summarize surplus_vprime_00 if (alpha - psi) > .05
local ave_surplus_vprime_00_05 = r(mean) 

** low ACG surplus
summarize surplus_v if sum_concurrent_risk <= `acg_HL_cutoff'
local ave_surplus_v_low = r(mean)
summarize surplus_vprime if sum_concurrent_risk <= `acg_HL_cutoff'
local ave_surplus_vprime_low = r(mean) 
summarize surplus_v if (alpha - psi) > .05 & sum_concurrent_risk <= `acg_HL_cutoff'
local ave_surplus_v_05_low = r(mean) 
summarize surplus_vprime if (alpha - psi) > .05 & sum_concurrent_risk <= `acg_HL_cutoff'
local ave_surplus_vprime_05_low = r(mean) 
summarize surplus_v_00 if sum_concurrent_risk <= `acg_HL_cutoff'
local ave_surplus_v_00_low = r(mean) 
summarize surplus_vprime_00 if sum_concurrent_risk <= `acg_HL_cutoff'
local ave_surplus_vprime_00_low = r(mean) 
summarize surplus_v_00 if (alpha - psi) > .05 & sum_concurrent_risk <= `acg_HL_cutoff'
local ave_surplus_v_00_05_low = r(mean) 
summarize surplus_vprime_00 if (alpha - psi) > .05 & sum_concurrent_risk <= `acg_HL_cutoff'
local ave_surplus_vprime_00_05_low = r(mean) 

** high ACG surplus
summarize surplus_v if sum_concurrent_risk > `acg_HL_cutoff'
local ave_surplus_v_high = r(mean)
summarize surplus_vprime if sum_concurrent_risk > `acg_HL_cutoff'
local ave_surplus_vprime_high = r(mean) 
summarize surplus_v if (alpha - psi) > .05 & sum_concurrent_risk > `acg_HL_cutoff'
local ave_surplus_v_05_high = r(mean) 
summarize surplus_vprime if (alpha - psi) > .05 & sum_concurrent_risk > `acg_HL_cutoff'
local ave_surplus_vprime_05_high = r(mean) 
summarize surplus_v_00 if sum_concurrent_risk > `acg_HL_cutoff'
local ave_surplus_v_00_high = r(mean) 
summarize surplus_vprime_00 if sum_concurrent_risk > `acg_HL_cutoff'
local ave_surplus_vprime_00_high = r(mean) 
summarize surplus_v_00 if (alpha - psi) > .05 & sum_concurrent_risk > `acg_HL_cutoff'
local ave_surplus_v_00_05_high = r(mean) 
summarize surplus_vprime_00 if (alpha - psi) > .05 & sum_concurrent_risk > `acg_HL_cutoff'
local ave_surplus_vprime_00_05_high = r(mean) 

** non-tiered prices (only non-fee adjusted)
generate surplus_v_notiering = log(expV_notiering) / (alpha - psi)
generate surplus_v_00_notiering = log(expV_notiering + 1) / (alpha - psi)
summarize surplus_v_notiering
local ave_surplus_v_notiering = r(mean) 
summarize surplus_v_notiering if (alpha - psi) > .05
local ave_surplus_v_05_notiering = r(mean) 
summarize surplus_v_00_notiering
local ave_surplus_v_00_notiering = r(mean) 
summarize surplus_v_00_notiering if (alpha - psi) > .05
local ave_surplus_v_00_05_notiering = r(mean) 

* Saving results

** start saving
file open myfile using "../../../../tablesandfigures/release/fee_sg_cs/SG_CS_results.txt", write replace
file write myfile "Type, " "Consumer Surplus" _n

** full sample
file write myfile "CS: Full Sample; No Outside Option; Zero Fees, " "`ave_surplus_v'" _n
file write myfile "CS: Full Sample; No Outside Option; Fees, "  "`ave_surplus_vprime'" _n
file write myfile "CS: >.05 ; No Outside Option; Zero Fees, "  "`ave_surplus_v_05'" _n 
file write myfile  "CS: >.05 ; No Outside Option; Fees, "  "`ave_surplus_vprime_05'" _n 
file write myfile  "CS: Full Sample; Outside Option; Zero Fees, "  "`ave_surplus_v_00'" _n 
file write myfile  "CS: Full Sample; Outside Option; Fees, "  "`ave_surplus_vprime_00'" _n 
file write myfile  "CS: >.05 ; Outside Option; Zero Fees, "  "`ave_surplus_v_00_05'" _n 
file write myfile  "CS: >.05 ; Outside Option; Fees, "  "`ave_surplus_vprime_00_05'" _n

** low ACG subset
file write myfile "CS: Full Sample; No Outside Option; Zero Fees; Low ACG, " "`ave_surplus_v_low'" _n
file write myfile "CS: Full Sample; No Outside Option; Fees; Low ACG, "  "`ave_surplus_vprime_low'" _n
file write myfile "CS: >.05 ; No Outside Option; Zero Fees; Low ACG, "  "`ave_surplus_v_05_low'" _n 
file write myfile  "CS: >.05 ; No Outside Option; Fees; Low ACG, "  "`ave_surplus_vprime_05_low'" _n 
file write myfile  "CS: Full Sample; Outside Option; Zero Fees; Low ACG, "  "`ave_surplus_v_00_low'" _n 
file write myfile  "CS: Full Sample; Outside Option; Fees; Low ACG, "  "`ave_surplus_vprime_00_low'" _n 
file write myfile  "CS: >.05 ; Outside Option; Zero Fees; Low ACG, "  "`ave_surplus_v_00_05_low'" _n 
file write myfile  "CS: >.05 ; Outside Option; Fees; Low ACG, "  "`ave_surplus_vprime_00_05_low'" _n

** high ACG subset
file write myfile "CS: Full Sample; No Outside Option; Zero Fees; High ACG, " "`ave_surplus_v_high'" _n
file write myfile "CS: Full Sample; No Outside Option; Fees; High ACG, "  "`ave_surplus_vprime_high'" _n
file write myfile "CS: >.05 ; No Outside Option; Zero Fees; High ACG, "  "`ave_surplus_v_05_high'" _n 
file write myfile  "CS: >.05 ; No Outside Option; Fees; High ACG, "  "`ave_surplus_vprime_05_high'" _n 
file write myfile  "CS: Full Sample; Outside Option; Zero Fees; High ACG, "  "`ave_surplus_v_00_high'" _n 
file write myfile  "CS: Full Sample; Outside Option; Fees; High ACG, "  "`ave_surplus_vprime_00_high'" _n 
file write myfile  "CS: >.05 ; Outside Option; Zero Fees; High ACG, "  "`ave_surplus_v_00_05_high'" _n 
file write myfile  "CS: >.05 ; Outside Option; Fees; High ACG, "  "`ave_surplus_vprime_00_05_high'" _n

** non-tiered
file write myfile "CS: Full Sample; No Outside Option; Zero Fees; Non-tiered, " "`ave_surplus_v_notiering'" _n
file write myfile "CS: >.05 ; No Outside Option; Zero Fees; Non-tiered, "  "`ave_surplus_v_05_notiering'" _n 
file write myfile  "CS: Full Sample; Outside Option; Zero Fees; Non-tiered, "  "`ave_surplus_v_00_notiering'" _n 
file write myfile  "CS: >.05 ; Outside Option; Zero Fees; Non-tiered, "  "`ave_surplus_v_00_05_notiering'" _n 
file close myfile
